<?php

namespace App\Controller;

// +----------------------------------------------------------------------
// | 文件 AjaxMallController.class.php
// +----------------------------------------------------------------------
// | 说明 商城信息
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class AjaxMallController extends AjaxAppController
{
	static $text_map = [
		'type' 		=> ['exchange' => '兑换', 'gamble' => '抽奖'],
		'entity'	=> ['virtual'  => '虚拟', 'real'   => '实物'],
	];

	static $gift_filter = ['name','type','entity','score','game_id','status','content','flow','explain','pics','create_time','stock','hot','id'];

	/**
	 * @api {get} /App/AjaxMall/index  积分商城首页信息
	 * @apiVersion 4.0.4
	 * @apiName index
	 * @apiGroup AjaxMall
	 *
	 * @apiParam {String} appToken Users auth Token.
	 *
	 * @apiSuccess {String} info  show message
	 * @apiSuccess {Number} status  status code.
	 * @apiSuccess {Object} data  extend datas.
	 *
	 * @apiSuccessExample Success-Response:
	 *	HTTP/1.1 200 OK
	 *     {
	 *			"data": "",
	 *			"info": "",
	 *			"status": 1,
	 *	}
	 *
	 * @apiError UserNotFound The id of the User was not found.
	 *
	 * @apiErrorExample Error-Response:
	 *	HTTP/1.1 200 OK
	 *		{
	 *			"status": 0,
	 *			"info": "",
	 *			"data": "",
	 *		}
	 */
	public function index($appToken = '')
	{
		$this->checkUser($appToken);
		$ads  = event('Ad')->getAdsByCode('mallbanner');
		$ads = field_filter($ads, ['image', 'link', 'name'], true, ['image']);
		$user = event('User')->info($this->uid);

		#hot  ajax api/gift/hot
		$hot = event('Gift')->getHot(3);
		
		#recomd  ajax  api/gift/recommend
		$recomd = event('Gift')->getRecomd();
		$hot    = field_filter($hot, self::$gift_filter, true);
 		$recomd = field_filter($recomd, self::$gift_filter, true);
		int_to_string($hot, self::$text_map);
		int_to_string($recomd, self::$text_map);

 		// $hot = array_values($hot);
 		// $recomd = array_values($recomd);
 		
		$data = compact('ads', 'user', 'hot', 'recomd');
		$this->response(1, $data);
	 
	}

	// 商品详情 TODO: apidoc
	public function show($id = 0, $appToken = '')
	{
		$this->checkUser($appToken);
		$gift = event('Gift')->detail($id);
		$user = event('User')->info($this->uid);
		$gift = field_filter($gift, self::$gift_filter);


		$gifts = [$gift];
		int_to_string($gifts, self::$text_map);
		$gift = $gifts[0];

		$this->response(1, compact('gift', 'user'));
	}


	// 积分记录
	public function score_log($p = 1, $appToken = '')
	{
		$this->checkUser($appToken);
		$p = I('p', 1, 'intval');
		$p = $p > 0 ? $p : 1;
		$list = $this->getScorelogList($p);
		$data = [
			'count' => count($list),
			'items' => field_filter($list, ['memo', 'score', 'create_time'], true),
		];
		$this->response(1, $data);
	}

	// 兑换记录
	public function log($p = 1, $appToken = '')
	{
		$this->checkUser($appToken);
		$p = I('p', 1, 'intval');
		$p = $p > 0 ? $p : 1;
		$list = $this->getLogList($p);
		foreach ($list as $key => &$value) {
			$value['gift']['score'] = !empty($value['object_data']['score']) ? $value['object_data']['score'] : $value['gift']['score'];
		}
		$data = [
			'count' => count($list),
			'items' => field_filter($list, ['bindtime', 'key', 'gift', 'object_data'], true),
		];
		$this->response(1, $data);
	}



	// 获取积分记录
	protected function getScorelogList($p = 1)
	{
		$map = [
			'uid' => (int)$this->uid
		];
		$list = service('ScoreLog')->where($map)->order('create_time desc')->limit(10)->page($p)->select();
		return $list;
	}

	// 获取兑换记录
	protected function getLogList($p = 1)
	{
		$map = [
			'uid' 			=> (int)$this->uid,
			'object_type' 	=> 'gift',
		];
		$codes = service('Codes')->where($map)->order('bindtime desc')->limit(10)->page($p)->select();
		$gift_ids = array_column($codes, 'object_id');
		$gifts = service('Gift')->where(['id' => ['in', $gift_ids] ])->select();
		$gifts = field_filter($gifts, self::$gift_filter, true);
		int_to_string($gifts, self::$text_map);
		$gifts = array_column($gifts, null, 'id');
		foreach ($codes as &$value) {
			$value['gift'] = $gifts[$value['object_id']];
 		} 
		return $codes;
	}

}